//: Playground - noun: a place where people can play

import UIKit

// 1、定义 subscript

// 2、需求: 获取字符串第2个字符


extension String {

    subscript(n:Int) -> Character {
    
        let index = self.characters.index(self.characters.startIndex, offsetBy: n)
        
        return self.characters[index];
    }
    
    subscript(begin: Int, end: Int) -> String {
    
        let range = self.index(startIndex, offsetBy: begin)...self.index(startIndex, offsetBy: end);
        return self[range];
    }
    
    subscript(range: ClosedRange<Int>) -> String {
    
        let range = self.index(startIndex, offsetBy: range.lowerBound)...self.index(startIndex, offsetBy: range.upperBound)
        return self[range]
    }
}

let string = "真2的，真阳不好吧 哈哈哈"

let c = string[1]

let subString = string[2,4]
let sub = string[2...6]


// 截取范围内的字符串

/**
 1.下标的参数不能是inout 修饰的类型
 2.类或者结构体可以提供任意多个下标实现
 3.下标也是可以在枚举中使用的
 */
